const { DataTypes } = require('sequelize')

module.exports = (sequelize) => {
  const Product = sequelize.define('Product', {
    id: {
      type: DataTypes.BIGINT.UNSIGNED,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '商品名称'
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '商品描述'
    },

    original_price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: '原价'
    },
    image_url: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: '商品图片'
    },
    category: {
      type: DataTypes.ENUM('print', 'copy', 'service', 'goods'),
      allowNull: false,
      comment: '分类'
    },
    type: {
      type: DataTypes.ENUM('bw_print', 'color_print', 'copy', 'binding', 'laminating', 'layout', 'office_supplies'),
      allowNull: false,
      comment: '类型'
    },
    paper_size: {
      type: DataTypes.ENUM('A4', 'A3', 'A5', 'other'),
      allowNull: true,
      comment: '纸张尺寸'
    },
    paper_type: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '纸张类型'
    },
    color_type: {
      type: DataTypes.ENUM('bw', 'color', 'both'),
      allowNull: true,
      defaultValue: 'bw',
      comment: '颜色类型'
    },

    stock_quantity: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: '库存数量'
    },
    stock_warning: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 100,
      comment: '库存预警值'
    },
    sales_count: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: '销售数量'
    },

    points_price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      comment: '积分价格'
    },


    is_new: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否新品'
    },
    is_hot: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: '是否热销'
    },
    sort_order: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '排序'
    },
    status: {
      type: DataTypes.TINYINT,
      allowNull: false,
      defaultValue: 1,
      comment: '状态: 0-下架, 1-上架'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '创建时间'
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '更新时间'
    }
  }, {
    tableName: 'products',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      {
        name: 'idx_category',
        fields: ['category']
      },
      {
        name: 'idx_type',
        fields: ['type']
      },
      {
        name: 'idx_status',
        fields: ['status']
      },
      {
        name: 'idx_sort_order',
        fields: ['sort_order']
      }
    ]
  })

  Product.associate = (models) => {
    Product.hasMany(models.OrderItem, {
      foreignKey: 'product_id',
      as: 'orderItems'
    })
  }

  return Product
}